<?php

namespace app\common\dao\system;

use app\common\dao\BaseDao;
use app\traits\CategoryDao;
use app\common\model\system\SystemRegionModel;

class SystemRegionDao extends BaseDao
{
    use CategoryDao;

    /**
     * @return SystemRegionModel
     */
    protected function getModel(): string
    {
        return SystemRegionModel::class;
    }

    public function search(array $where)
    {
        return SystemRegionModel::getDB()
            ->when(isset($where['level']) && $where['level'] !== '', function ($query) use ($where) {
                $query->where('level', $where['level']);
            })
            ->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
                $query->where('pid', $where['pid']);
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('merger_name|name|area_code', '%' . trim($where['keywords']) . '%');
            });
    }

}
